#include<stdio.h>
#include<stdlib.h>
typedef struct TNode *Position;
typedef Position Tree;
struct TNode
{
    int number;
    int Height;
    Tree Left, Right, Middle;
    int Branch;
};
struct parameter
{
    int MaxBranch;
    int Count;
    int num;
    int height;
};
typedef struct parameter *Tmp;
Tree Forest[100];
int tree[100], count[100];
void Insert(int t, int l , int m, int r)
{
    if(l)
    {
        Forest[t]->Left = Forest[l-1];
        Forest[t]->Left->Height = Forest[t]->Height + 1;
        Forest[t]->Branch++;
    } 
    if(m)
    {
        Forest[t]->Middle = Forest[m-1];
        Forest[t]->Middle->Height = Forest[t]->Height + 1;
        Forest[t]->Branch++;
    }
    if(r)
    {
        Forest[t]->Right = Forest[r-1];
        Forest[t]->Right->Height = Forest[t]->Height + 1;
        Forest[t]->Branch++;
    }
}

Tmp PreTraversal(Tree p, Tmp tmp)
{
    if(p == NULL) return 0;
    else
    {
        tmp->Count++;
        if(p->Branch > tmp->MaxBranch)
        {
            tmp->MaxBranch = p->Branch;
            tmp->num = 0;
            tree[tmp->num] = p->number; count[tmp->num] = tmp->Count;
            tmp->num++;
        }
        else if(p->Branch == tmp->MaxBranch)
        {
            if(p->Height > tmp->height)
            {
                tmp->height =p->Height;
                tmp->num = 0;
                tree[tmp->num] = p->number; count[tmp->num] = tmp->Count;
                tmp->num++;
            }
            else if(p->Height == tmp->height)
            {
                tree[tmp->num] = p->number; count[tmp->num] = tmp->Count;
                tmp->num++;
            }
        }
    }
    if(p->Left) tmp = PreTraversal(p->Left, tmp);
    if(p->Middle) tmp = PreTraversal(p->Middle, tmp);
    if(p->Right) tmp = PreTraversal(p->Right, tmp);
    return tmp;
}

int main()
{
    int n;
    scanf("%d", &n);
    for(int i=0;i<100;i++)
    {
        Forest[i] = (Tree)malloc(sizeof(struct TNode));
        Forest[i]->Left = Forest[i]->Middle =  Forest[i]->Right = NULL;
        Forest[i]->Branch = Forest[i]->Height = 0;
        tree[i] = count[i] = 0;
    }
    int t, l, m, r, root;
    scanf("%d %d %d %d", &t, &l, &m, &r);   
    root = t-1;
    Forest[root]->number = root+1;
    Insert(root, l, m, r);
    while(--n)
    {
        scanf("%d %d %d %d", &t, &l, &m, &r);
        t--;
        Forest[t]->number = t+1;       
        Insert(t, l, m, r);
    }
    Tmp tmp = (Tmp)malloc(sizeof(struct parameter));
    tmp->Count = tmp->height = tmp->MaxBranch = tmp->num = 0;
    tmp = PreTraversal(Forest[root], tmp);
    for(int i=0;i<tmp->num;i++)
        printf("%d %d\n", tree[i], count[i]);
    return 0;
}